#ifndef __CROSSFIRE_MATH_BOUNDBOX
#define __CROSSFIRE_MATH_BOUNDBOX

namespace Math
{

class CCF_BoundingBox
{
private:
	D3DXVECTOR3 minPoint;
    D3DXVECTOR3 maxPoint;
public:
	CCF_BoundingBox ( const D3DXVECTOR3& v1, const D3DXVECTOR3& v2 )
    {
        if ( v1.x < v2.x )
        {
            minPoint.x = v1.x;
            maxPoint.x = v2.x;
        }
        else
        {
            minPoint.x = v2.x;
            maxPoint.x = v1.x;
        }

        if ( v1.y < v2.y )
        {
            minPoint.y = v1.y;
            maxPoint.y = v2.y;
        }
        else
        {
            minPoint.y = v2.y;
            maxPoint.y = v1.y;
        }

        if ( v1.z < v2.z )
        {
            minPoint.z = v1.z;
            maxPoint.z = v2.z;
        }
        else
        {
            minPoint.z = v2.z;
            maxPoint.z = v1.z;
        }
    }

	bool contains(const D3DXVECTOR3& pos ) const
    {
        return pos.x >= minPoint.x && pos.x <= maxPoint.x &&
               pos.y >= minPoint.y && pos.y <= maxPoint.y &&
               pos.z >= minPoint.z && pos.z <= maxPoint.z;
    }

	bool intersects ( const CCF_BoundingBox& box ) const
	{
		if (( maxPoint.x < box.minPoint.x) || (minPoint.x > box.maxPoint.x) )
			return false;

		if (( maxPoint.y < box.minPoint.y) || (minPoint.y > box.maxPoint.y) )
			return false;

		if (( maxPoint.z < box.minPoint.z) || (minPoint.z > box.maxPoint.z) )
			return false;

		return true;
	}

	D3DXVECTOR3	getMin () const
	{
		return minPoint;
	}

	D3DXVECTOR3	getMax () const
	{
		return maxPoint;
	}
	
	void	move  ( const D3DXVECTOR3& v )
	{
		minPoint += v;
		maxPoint += v;
	}

	D3DXVECTOR3 getVertex ( int index ) const
    {
        return D3DXVECTOR3( index & 1 ? maxPoint.x : minPoint.x,
                          index & 2 ? maxPoint.y : minPoint.y,
                          index & 4 ? maxPoint.z : minPoint.z );
    }

	D3DXVECTOR3 getCenter () const
	{
		return (minPoint + maxPoint) * 0.5f;
	}

	float getSize () const
	{
		return maxPoint.x - minPoint.x + maxPoint.y - minPoint.y + maxPoint.z - minPoint.z;
	}
};

};


#endif